myApp.controller('brandListController',function($scope,toaster,$uibModal,$filter,$state,$log,$serBrand,FileUploader,$serImgUpload){
    var ctrl = this;
    ctrl.vm = {
        data:{},
        search:{
            id:'',
            code:'',
            name:'',
            status:'',
        },
        searchFn:function(){
            vm.init();
        },
        reset:function(){
            ctrl.vm.search.id='';
            ctrl.vm.search.code='';
            ctrl.vm.search.name='';
            ctrl.vm.search.status='';
            vm.init();
        },
    };

    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };
    $scope.model = {
        open: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'addBrand.html',
                controller: 'addBrandCtrl',
                windowClass: 'BrandOpen',
                size: 'lg',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        open1: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'upBrand.html',
                controller: 'upBrandCtrl',
                windowClass: 'BrandOpen',
                size: 'lg',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        del:function (data) {
            var modalInstance = $uibModal.open({
                template: '<div class="modal-header"><h3 class="modal-title">删除确认</h3></div>' +
                '<div class="modal-footer"><button class="btn btn-default" ng-click="cancel()">Cancel</button>' +
                '<button class="btn btn-primary" ng-click="ok()">OK</button></div>',
                controller: 'delBrandController',
                windowClass: 'brandList',
                size: 'sm',
            });
            modalInstance.result.then(function (res) {
                var type='';
                if(res){
                    $serBrand.delIt({
                        id:data.id
                    }).then(function(res){
                        switch (res.code){
                            case 1:type = 'success';break;
                            default:type = 'error';break;
                        }
                        toaster.pop(type, '信息', res.message);
                        vm.init();
                    })
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    var vm ={
        init:function(page){
            $serBrand.getList({
                page:page||1,
                size:$scope.placement.selected,
                id:ctrl.vm.search.id||'',
                name:ctrl.vm.search.name||'',
                status:ctrl.vm.search.status||'',
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                    $scope.vm.page = res.addtion_data;
                }else{
                    ctrl.vm.data ="";
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
    myApp.controller('addBrandCtrl', function($scope, $uibModalInstance, info, $serBrand, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            sort:'',
            value:'',
            type:'',
            button_word:'',
            word_2:'',
            word_1:'',
        }
        //图片上传
        var uploader = $scope.uploader = $serImgUpload.setImgUpload('brand_',true);
        $serImgUpload.putImgUpload(uploader,'brand',function(){
            $serImgUpload.upload(uploader);
        });
        uploader.onSuccessItem = function(fileItem) {
            $scope.fileItem = fileItem.file.name;
        };
        //图片上传end

        $scope.ok = function () {
            $serBrand.addIt({
                logo:$scope.fileItem,
                status:$scope.vm.status,
                sort:$scope.vm.sort,
                name:$scope.vm.name,
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });

    myApp.controller('upBrandCtrl', function($scope, $uibModalInstance, info,$serBrand, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            sort:'',
            value:'',
            type:'',
            button_word:'',
            word_2:'',
            word_1:'',
        }


        function init(){
            $scope.fileItem = info.logo
            $scope.vm.status = info.status
            $scope.vm.sort = info.sort
            $scope.vm.name = info.name

        };
        init();

        //图片上传
        var uploader = $scope.uploader = $serImgUpload.setImgUpload('brand_',true);

        $scope.clearItems = function () {
            $serImgUpload.putImgUpload(uploader,'brand',function(){
                $serImgUpload.upload(uploader);

            });
            uploader.onSuccessItem = function(fileItem) {
                $scope.fileItem = fileItem.file.name;
            };
        }


        $scope.ok = function () {
            $serBrand.updateIt({
                id:info.id,
                name:$scope.vm.name,
                logo:$scope.fileItem,
                status:$scope.vm.status,
                sort:$scope.vm.sort,
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });

});
myApp.controller('delBrandController', ['$scope', '$uibModalInstance', function($scope,$uibModalInstance) {
    $scope.ok = function () {
        $uibModalInstance.close(true);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);


